package com.itheima.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.Log;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.LogVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.LogService;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class LogsManager {
    @Reference
    private LogService logService;

    @Reference
    private UserInfoService userInfoService;

    public PageBeanVo logs(Integer page, Integer pagesize) {
        PageBeanVo pageBeanVo = logService.logs(page, pagesize);
        List<Log> logList = (List<Log>) pageBeanVo.getItems();
        List<LogVo> logVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(logList)) {
            for (Log log : logList) {
                LogVo logVo = new LogVo();
                UserInfo userInfo = userInfoService.findById(log.getUserId());
                if (userInfo != null) {
                    logVo.setId(log.getId());
                    logVo.setName(userInfo.getNickname());
                    logVo.setDate(log.getLogTime());
                    logVo.setPlace(log.getPlace());
                    logVo.setEquipment(log.getEquipment());
                    if (StrUtil.equals(log.getType(), "0101")) {
                        logVo.setType("登录");
                    } else if (StrUtil.equals(log.getType(), "0102")) {
                        logVo.setType("注册");
                    } else if (StrUtil.equals(log.getType(), "0201")) {
                        logVo.setType("发动态");
                    } else if (StrUtil.equals(log.getType(), "0202")) {
                        logVo.setType("浏览动态");
                    } else if (StrUtil.equals(log.getType(), "0203")) {
                        logVo.setType("动态点赞");
                    } else if (StrUtil.equals(log.getType(), "0204")) {
                        logVo.setType("动态喜欢");
                    } else if (StrUtil.equals(log.getType(), "0205")) {
                        logVo.setType("评论");
                    } else if (StrUtil.equals(log.getType(), "0206")) {
                        logVo.setType("动态取消点赞");
                    } else if (StrUtil.equals(log.getType(), "0207")) {
                        logVo.setType("动态取消喜欢");
                    } else if (StrUtil.equals(log.getType(), "0301")) {
                        logVo.setType("发小视频");
                    } else if (StrUtil.equals(log.getType(), "0302")) {
                        logVo.setType("小视频点赞");
                    } else if (StrUtil.equals(log.getType(), "0303")) {
                        logVo.setType("小视频取消点赞");
                    } else if (StrUtil.equals(log.getType(), "0304")) {
                        logVo.setType("小视频评论");
                    }
                    logVos.add(logVo);
                }

            }

        }
        pageBeanVo.setItems(logVos);
        return pageBeanVo;
    }
}
